package com.xique.door.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xique.common.core.utils.StringUtils;
import com.xique.door.bean.domain.CardUserDevice;
import com.xique.door.bean.domain.UserDevice;
import com.xique.door.bean.search.UserDeviceListSearch;
import com.xique.door.bean.search.UserDeviceSearch;
import com.xique.door.mapper.UserDeviceMapper;
import com.xique.door.service.IUserDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 用户设备表Service业务层处理
 *
 * @author ww
 * @date 2021-06-17
 */
@Service
public class UserDeviceServiceImpl implements IUserDeviceService
{
    @Autowired
    private UserDeviceMapper userDeviceMapper;

    @Override
    public List<UserDevice> selectUserDeviceListByUserIdsAndDeviceIds(Long parkId, List<Long> userIds, List<Long> deviceIds) {
        if (userIds.isEmpty() || deviceIds.isEmpty()) {
            return new ArrayList<>();
        }
        return userDeviceMapper.selectUserDeviceListByUserIdsAndDeviceIds(parkId, userIds, deviceIds);
    }

    /**
     * 查询用户设备表列表
     *
     * @param userDevice 用户设备表
     * @return 用户设备表
     */
    @Override
    public List<UserDevice> selectUserDeviceList(UserDevice userDevice)
    {
        return userDeviceMapper.selectUserDeviceList(userDevice);
    }

    /**
     * 查找对应的用户设备绑定信息
     * @author shd
     * @date 2021/7/5 11:36
     * @param search 入参
     * @return java.util.List<com.jzy.oms.access.permissionGroup.userDevice.bean.domain.UserDevice>
     */
    @Override
    public List<UserDevice> selectUserDeviceListBySearch(UserDeviceSearch search) {
        return userDeviceMapper.selectUserDeviceListBySearch(search);
    }

    @Override
    public List<UserDevice> selectUserDeviceListByBuild(UserDeviceSearch search) {
        return userDeviceMapper.selectUserDeviceListByBuild(search);
    }

    /**
     * 批量新增用户设备表
     * @author shd
     * @date 2021/7/6 10:22
     * @param list
     * @return int
     */
    @Override
    public int batchInsertUserDevice(List<UserDevice> list) {
        if (list.isEmpty()) {
            return 1;
        }
        return userDeviceMapper.batchInsertUserDevice(list);
    }

    @Override
    public int batchDeleteUserDevice(List<Long> ids) {
        if (ids.isEmpty()) {
            return 1;
        }
        return userDeviceMapper.deleteUserDeviceByIds(ids);
    }

    /**
     * 查询用户设备表
     *
     * @param id 用户设备表ID
     * @return 用户设备表
     */
    @Override
    public UserDevice selectUserDeviceById(Long id)
    {
        return userDeviceMapper.selectById(id);
    }


    @Override
    public List<UserDevice> selectUserDeviceList(UserDeviceListSearch search) {
        QueryWrapper<UserDevice> wrapper = new QueryWrapper<>();
        if (null != search.getUserId()) {
            wrapper.eq("user_id", search.getUserId());
        }
        if (CollectionUtils.isNotEmpty(search.getUserIds())) {
            wrapper.in("user_id", search.getUserIds());
        }
        if (null != search.getDeviceId()) {
            wrapper.eq("device_id", search.getDeviceId());
        }
        if (CollectionUtils.isNotEmpty(search.getDeviceIds())) {
            wrapper.in("device_id", search.getDeviceIds());
        }
        if (StringUtils.isNotNull(search.getIds())) {
            wrapper.in("id", search.getIds());
        }
        return userDeviceMapper.selectList(wrapper);
    }

    @Override
    public List<UserDevice> selectUserDeviceListWithDevice(UserDeviceListSearch search) {
        return userDeviceMapper.selectUserDeviceListWithDevice(search);
    }

    /**
     * 新增用户设备表
     *
     * @param userDevice 用户设备表
     * @return 结果
     */
    @Override
    public int insertUserDevice(UserDevice userDevice)
    {
        return userDeviceMapper.insertUserDevice(userDevice);
    }

    /**
     * 修改用户设备表
     *
     * @param userDevice 用户设备表
     * @return 结果
     */
    @Override
    public int updateUserDevice(UserDevice userDevice)
    {
        return userDeviceMapper.updateUserDevice(userDevice);
    }

    @Override
    public int batchUpdateUserDevice(List<UserDevice> userDevices)
    {
        return userDeviceMapper.batchUpdateUserDevice(userDevices);
    }

    /**
     * 批量删除用户设备表
     *
     * @param ids 需要删除的用户设备表ID
     * @return 结果
     */
    @Override
    public int deleteUserDeviceByIds(Long[] ids)
    {
        return userDeviceMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除用户设备表信息
     *
     * @param id 用户设备表ID
     * @return 结果  77777555666
     */
    @Override
    public int deleteUserDeviceById(Long id)
    {
        return userDeviceMapper.deleteById(id);
    }

    @Override
    public List<CardUserDevice> selectCardUserDeviceList() {
        return userDeviceMapper.selectCardUserDeviceList();
    }

    @Override
    public int deleteUserDeviceByDeviceId(Long deviceId) {
        return userDeviceMapper.deleteUserDeviceByDeviceId(deviceId);
    }

    @Override
    public List<UserDevice> selectUserDeviceByLists(Long id,Long parkId) {
        return userDeviceMapper.selectUserDeviceByLists(id,parkId);
    }


}
